package com.yx.cloud.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yx.cloud.Interface.BounceCallBack;
import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.activity.MessageCenterDetailsActivity;
import com.yx.cloud.adapter.MessageInformAdapter;
import com.yx.cloud.bean.MessageCenterBean;
import com.yx.cloud.header.NormalBounceHandler;
import com.yx.cloud.view.BounceLayout;
import com.yx.cloud.view.DefaultHeader;
import com.yx.cloud.view.DividerItemDecoration;
import com.iot.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 消息通知
 */
public class MessageInformFragment extends Fragment {
    private final static String TAG = "MessageInformFragment";
    private View view;
    private BounceLayout bl_messageInform;
    private FrameLayout fl_message_container;
    private RecyclerView mListView;
    private ImageView iv_noDataIcon;
    private TextView tv_noData;
    public MessageInformAdapter miAdapter;
    private MessageCenterBean bean;
    private List<MessageCenterBean> list;
    private LinearLayout ll_noDataLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.i("消息 MessageInformFragment onCreateView");
        view = inflater.inflate(R.layout.message_inform_fragment, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        LogUtils.i("mf MessageWarningFragment initView");
        ll_noDataLayout = view.findViewById(R.id.ll_noMessageInformDataLayout);
        iv_noDataIcon = view.findViewById(R.id.iv_noMessageInformDataIcon);
        tv_noData = view.findViewById(R.id.tv_noMessageInformData);
        mListView = view.findViewById(R.id.lv_inform_message);
        bl_messageInform = view.findViewById(R.id.bl_inform_message);
        fl_message_container = view.findViewById(R.id.fl_message_container_inform);
        bl_messageInform.setBounceHandler(new NormalBounceHandler(), mListView);//设置滚动冲突的控制类
        //自定义事件分发处理
        bl_messageInform.setEventForwardingHelper((downX, downY, moveX, moveY) -> true);
        bl_messageInform.setHeaderView(new DefaultHeader(getActivity()), fl_message_container);//设置刷新头，null意味着没有刷新头，不调用该函数意为着空
       // bl_messageInform.setFooterView(new DefaultFooter(getActivity()), fl_message_container);
        bl_messageInform.setBounceCallBack(new BounceCallBack() {
            @Override
            public void startRefresh() {
                requestNotification();
            }

            @Override
            public void startLoadingMore() {
                //requestNotification(messageAdapter.getCount(), 10, false);
            }
        });
    }

    private void initData() {
        iv_noDataIcon.setImageResource(R.mipmap.no_message_icon);//无消息图标
        tv_noData.setText(getString(R.string.H_NoMessage));

        list = new ArrayList<>();
        miAdapter = new MessageInformAdapter(getActivity(), list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mListView.setLayoutManager(layoutManager);
        mListView.setAdapter(miAdapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, 200, 50);
        itemDecoration.setDrawable(getActivity().getDrawable(R.drawable.list_item_divider));
        mListView.addItemDecoration(itemDecoration);

        miAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                LogUtils.i("MessageCenterActivity onItemClick : " + position);
                MessageCenterBean messageCenterBean = list.get(position);
                Intent intent = new Intent(getActivity(), MessageCenterDetailsActivity.class);
                intent.putExtra("source", "inform");
                intent.putExtra("details", messageCenterBean);
                startActivity(intent);
            }
        });
        miAdapter.setChecked(new MessageInformAdapter.onChecked() {
            @Override
            public void CheckedChanged(int position, boolean isChecked, CompoundButton buttonView) {
                LogUtils.i("MessageCenterActivity Checked : " + position + isChecked);

            }
        });
    }

    private void requestNotification() {
//        String time = StringUtils.getTime();
//        MessageParam param = new MessageParam();
//        param.setStart_timestamp(0);
//        param.setEnd_timestamp(Integer.parseInt(time));
//        param.setEvent_id("120");
//        param.setTotal_index(20);
//        param.setFrom_index(0);
//        AppContext.getDeviceService().getMessageList(param);
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onMessageEvent(Event event) {
//        if (event.getType() == Event.MESSAGE_TYPE_GET_MESSAGE_LIST) {
//           // bl_messageInform.setLoadingMoreCompleted();
//            bl_messageInform.setRefreshCompleted();
//            LogUtils.i("消息 MessageWarningFragment onMessageEvent");
//            if (event.isResult()) {
//                YuGongRespMessageListData data = (YuGongRespMessageListData) event.getContent();
//                List<YuGongRespMessageListData.MsgListBean> msg_list = data.getMsg_list();
//                if (msg_list.size() > 0) {
//                    mListView.setVisibility(View.VISIBLE);
//                    ll_noDataLayout.setVisibility(View.GONE);
//                    for (int i = 0; i < msg_list.size(); i++) {
//                        String event_id = msg_list.get(i).getEvent_id();//类型
//                        if (event_id.equals("120")) {
//                            bean = new MessageCenterBean();
//                            bean.setTime(msg_list.get(i).getC_time());
//                            bean.setTitle(msg_list.get(i).getTitle());
//                            bean.setContent(msg_list.get(i).getContent());
//                            bean.setEvent_desc(msg_list.get(i).getEvent_desc());
//                            list.add(bean);
//                        }
//                    }
//                } else {
////                    mListView.setVisibility(View.VISIBLE);
////                    ll_noDataLayout.setVisibility(View.GONE);
////                    for (int i = 0; i < 3; i++) {
////                        bean = new MessageCenterBean();
////                        bean.setTime(1678775533);
////                        bean.setTitle("启动机器遇到禁区。");
////                        bean.setContent("启动时机器太靠近虚拟墙磁条或则强磁场，请移动到新位置启动");
////                        bean.setEvent_desc("启动遇到禁区");
////                        list.add(bean);
////                    }
////                    miAdapter.updateData(list);
//                    mListView.setVisibility(View.GONE);
//                    ll_noDataLayout.setVisibility(View.VISIBLE);
//                }
//            }
//        }
//    }
}
